<?php
    session_start();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>会员管理系统</title>
    <style>
        .main{width:80%; margin:0; text-align:center}
        h2{font-size:16px}
        h2 a{color:navy; text-decoration; none;margin-right:15px}
        h2 a:last-child{margin-right: 0}
        h2 a:hover{color:brown; text-decoration:underline}  /*悬停hover效果 */
        .current{color:darkgreen}
        .red{color: red}
        .green{font-size:20px color: green}
        .black{font-size:20px color: black}
    </style>
</head>
<body>
    <div class="main">
        <form action="postReg.php" method="post" onsubmit="return check()">
            <table align="center" border="1" style="..." cellpadding="5" cellspacing="0">
                <tr>
                    <td align="right">用户名</td>
                    <td align="left"><input name="username" onblur="checkUsername()"><span class="red">*</span> <span id="usernameMsg"></span></td>
                </tr>
                <tr>
                    <td align="right">密码</td>
                    <td align="left"><input type="password" name="password"><span class="red">*</span></td>
                </tr>
                <tr>
                    <td align="right">确认密码</td>
                    <td align="left"><input type="password" name="cpassword"><span class="red">*</span></td>
                </tr>
                <tr>
                    <td align="right">邮箱</td>
                    <td align="left"><input name="email"><span class="red">*</span></td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="submit" value="提交">
                        <input type="reset" value="重置">
                    </td>
                </tr>
            </table>
        </form>
    </div>
    <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
    <script>
        function checkUsername() {
            let username = document.getElementsByName('username')[0].value.trim();
            let usernameRag = /^[a-zA-Z0-9]{3,10}$/;
            if(!usernameRag.test(username)) {
                // alert('请用户名，且只能由字母大小写和数字构成，长度为3-10个字符');
                $("#usernameMsg").text("");
                return false;
            }
            $.ajax({
                url:"checkUsername.php",
                type:"post",
                dataType:"json",
                data:{username:username},
                success:function (data) {
                    if(data.code == 0) {
                        //不可用
                        // $("#usernameMsg").text(data.msg).addClass('green');
                        $("#usernameMsg").text("重复名").removeClass('black').addClass('green');
                    }
                    if(data.code == 2) {
                        // $("#usernameMsg").text(data.msg).addClass('black');
                        $("#usernameMsg").text("√").removeClass('green').addClass('black');
                        //可用
                    }
                },
                error:function () {
                    alert("网络错误！");
                }
            })
        }
        function check() {
            let username = document.getElementsByName('username')[0].value.trim();
            let password = document.getElementsByName('password')[0].value.trim();
            let cpassword = document.getElementsByName('cpassword')[0].value.trim();
            let email = document.getElementsByName('email')[0].value.trim();
            let usernameRag = /^[a-zA-Z0-9]{3,10}$/;
            if(!usernameRag.test(username)) {
                alert('请用户名，且只能由字母大小写和数字构成，长度为3-10个字符');
                return false;
            }
            //验证密码
            let passwordRag = /^[a-zA-Z0-9_*]{6,10}$/;
            if(!passwordRag.test(password)) {
                alert('密码必填，且只能由字母大小写和数字，以及 _ 和 * 构成，长度为3-10个字符');
                return false;
            } else if(password != cpassword) {
                    alert('两次填写的密码不相同！');
                    return false;
            }
            //邮箱验证
            let emailRag = /^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
            if (email.length > 0) {
                if(!emailRag.test(email)) {
                    alert('邮箱格式不正确');
                    return false;
                }
            }
            return true;
        }
    </script>
</body>
</html>